<template>
	<view>
		<view class="auth">
			<view class="title">
				请设置
				<text style="color: #f66ef1">6位数字</text>
				的交易密码
			</view>
			<view class="xx">
				<yi-code focus :width="640" hide="*" @onComplete="mm($event, 'one')"></yi-code>
			</view>
			<view class="title">请重复交易密码</view>
			<view class="xx">
				<yi-code :width="640" hide="*" @onComplete="mm($event, 'two')"></yi-code>
			</view>
		</view>
		<view class="btn1 qd" @click="ok">确定</view>
		<uni-popup ref="popup" :mask-click="false">
			<view class="bg-white popup-box">
				<view class="title">短信验证码至{{ mobile }}</view>
				<view class="xx">
					<yi-code focus :width="480" hide="*" @onComplete="mm($event, 'code')"></yi-code>
				</view>
				<view class="auth-again">
					<text v-if="countdown > 0" class="time">{{ countdown }}秒后重新发送</text>
					<text v-if="countdown == 0" @tap="resend">重新发送</text>
					<!-- <text v-if="countdown < 50">没有收到验证码？</text> -->
				</view>
				<view class="btn-g">
					<view class="btn" @click="xx">取消</view>
					<view class="btn qd" @click="ok1">确定</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>
<script>
export default {
	data() {
		return {
			cTimer: null,
			countdown: 60,
			type: 1,
			mobile: '',
			mima: {
				one: '',
				two: '',
				code: ''
			}
		};
	},
	async onLoad({ type, mobile }) {
		this.mobile = mobile;
		this.type = type || 1;
	},
	methods: {
		sendMessage() {
			// await
			uni.request({
				url: '/wanlshop/sms/send',
				method: 'POST',
				data: {
					event: 'changepwd',
					mobile: this.mobile
				},
				success: (res) => {
					this.$wanlshop.msg('验证码发送成功');
					this.startTimer();
				}
			});
		},
		startTimer() {
			if (this.cTimer == null) {
				this.cTimer = setInterval(() => {
					this.countdown--;
					if (this.countdown == 0) {
						this.clearTimer();
					}
				}, 1000);
			}
		},
		clearTimer() {
			clearInterval(this.cTimer);
			this.cTimer = null;
		},
		resend() {
			this.startTimer();
			this.countdown = 60;
			this.sendMessage();
		},
		xx() {
			this.$refs.popup.close();
			this.mima.code = '';
		},
		mm(e, type) {
			this.mima[type] = e;
		},
		ok1() {
			if (!this.mima.code) {
				this.$wanlshop.msg('请填写验证码');
				this.loading = false;
				return;
			}
			uni.request({
				url: '/user/changePassword',
				method: 'POST',
				data: {
					newpassword: this.mima.one,
					mobile: this.mobile,
					captcha: this.mima.code
				},
				success: (res) => {
					uni.showToast({
						icon: 'none',
						title: '密码修改成功'
					});
					this.$wanlshop.back();
				},
				complete: () => {}
			});
		},
		async ok() {
			if (!this.mima.one) {
				this.$wanlshop.msg('请填写交易密码');
				this.loading = false;
				return;
			}
			if (this.mima.two != this.mima.one) {
				this.$wanlshop.msg('两次交易密码不同');
				this.loading = false;
				return;
			}
			if (this.type == 2) {
				this.$refs.popup.open();
				!this.cTimer && this.resend();
				return;
			}

			await uni.request({
				url: '/user/setPassword',
				method: 'POST',
				data: {
					password: this.mima.one,
					re_password: this.mima.two
				},
				success: (res) => {
					uni.showToast({
						icon: 'none',
						title: '密码设置成功'
					});
					this.$store.commit('statistics/edit', {
						is_pwd_method: 2
					});
					this.$wanlshop.back();
				},
				complete: () => {}
			});
		}
	}
};
</script>
<style scoped>
/* @import url('auth.css'); */
page {
	background-color: #f9f9f9 !important;
}
.auth {
	background-color: #fff;
	border-radius: 20rpx;
	width: 700rpx;
	margin: 0 auto;
	padding: 30rpx;
	display: flex;
	flex-direction: column;
	justify-content: space-around;
	.title {
		padding: 30rpx 0;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 30rpx;
		color: #000000;
		line-height: 32rpx;
		text-align: left;
		font-style: normal;
		text-transform: none;
	}
	.yi-code-show-item {
		width: 92rpx !important;
		height: 98rpx !important;
	}
}

.btn1 {
	font-family: Source Han Sans, Source Han Sans;
	font-weight: 350;
	font-size: 28rpx;
	color: #3d3d3d;
	line-height: 40rpx;
	text-align: left;
	font-style: normal;
	text-transform: none;
	width: 700rpx;
	height: 100rpx;
	background: #e9e9e9;
	margin: 100rpx auto 0;
	border-radius: 200rpx 200rpx 200rpx 200rpx;
	display: flex;
	justify-content: center;
	align-items: center;
}
.qd {
	background: #f66ef1;
	color: #fff;
}
.popup-box {
	width: 560rpx;
	height: 448rpx;
	background: #ffffff;
	border-radius: 40rpx;
	display: flex;
	flex-direction: column;
	padding: 38rpx;
	box-sizing: border-box;
	.title {
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 32rpx;
		color: #000000;
		line-height: 48rpx;
		font-style: normal;
		text-transform: none;
	}
	.xx {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.btn-g {
		justify-content: space-around;
		display: flex;
		.btn {
			font-family: Source Han Sans, Source Han Sans;
			font-weight: 350;
			font-size: 28rpx;
			color: #3d3d3d;
			line-height: 40rpx;
			text-align: left;
			font-style: normal;
			text-transform: none;
			width: 217rpx;
			height: 68rpx;
			background: #e9e9e9;
			border-radius: 200rpx 200rpx 200rpx 200rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}
		.qd {
			background: #f66ef1;
			color: #fff;
		}
	}

	.auth-again {
		padding: 30rpx 0;
	}

	.auth-again text {
		color: #ed6d0f;
	}

	.auth-again .time {
		color: #9a9a9a;
	}
}
/deep/.uni-transition {
	background-color: rgba(0, 0, 0, 0.3) !important;
}
</style>
